package com.mhxy.service.impl;

import com.mhxy.constant.PicConstants;
import com.mhxy.entity.FindPicResult;
import com.mhxy.enums.Coord;
import com.mhxy.handler.TaskProgressHandler;
import com.mhxy.service.AbstractTask;
import com.mhxy.util.FuzzyPicUtil;
import com.mhxy.util.MouseUtil;
import com.mhxy.util.ThreadUtil;
import com.sun.jna.platform.win32.WinDef;
import lombok.extern.slf4j.Slf4j;

/**
 * 帮派任务
 */
@Slf4j
public class FactionTaskImpl extends AbstractTask {

    private final String hwndName;
    
    private final double sim = 0.7;

    public FactionTaskImpl(WinDef.HWND hwnd_0, String hwndName) {
        super(hwnd_0);
        this.hwndName = hwndName;
    }

    @Override
    public Boolean before() throws Exception {
        clickEsc3();
        return goToJianYe();
    }

    @Override
    public Boolean execute() throws Exception {
        int count = 0;
        int notFoundCount = 0;
        while (true) {
            count++;
            // 是否停止任务
            if (!TaskProgressHandler.checkHwndUserThreadIsRun(hwndName)) {
                TaskProgressHandler.updateProgress(getTaskName(), "停止");
                return goToChangAn();
            }
            // 连续超过3次没找到任务就结束
            if (notFoundCount >= 3) {
                TaskProgressHandler.updateProgress(getTaskName(), "结束");
                return goToChangAn();
            }

            TaskProgressHandler.updateProgress(getTaskName(), count + "次");

            clickEsc1();
            // 下拉任务列表到最顶端
            MouseUtil.dragMove(hwnd_0, Coord.TASK_LIST_WHELL.getX(), Coord.TASK_LIST_WHELL.getY(), 0, 1000);

            String[] taskPics = new String[]{
                    PicConstants.帮派任务_拜访,
                    PicConstants.帮派任务_切磋,
                    PicConstants.帮派任务_宣读公告,
                    PicConstants.帮派任务_粮草,
                    PicConstants.帮派任务_战备,
                    PicConstants.帮派任务_物资,
                    PicConstants.帮派任务_除魔,
                    PicConstants.帮派任务_药材,
                    PicConstants.帮派任务_家园巡逻,
                    PicConstants.帮派任务_帮派巡逻,
                    PicConstants.帮派任务_挑战帮主
            };

            FindPicResult result = FuzzyPicUtil.findPicM(hwnd_0, taskPics, 1, sim);
            log.info("result={}", result);
            if (!result.isFinded()) {
                notFoundCount++;
                continue;
            }
            notFoundCount = 0;

            MouseUtil.lClickRandom(hwnd_0, result.getPicX(), result.getPicY());

            for (int i=0; i < taskPics.length; i++) {
                if (result.getFindResult()[i] != 1) {
                    continue;
                }

                switch (i) {
                    case 0:         // 帮派任务_拜访
                        baiFang();
                        break;
                    case 1:         // 帮派任务_切磋
                        qieCuo();
                        break;
                    case 2:         // 帮派任务_宣读公告
                        gongGao();
                        break;
                    case 3:         // 帮派任务_粮草
                        taskBuy();
                        break;
                    case 4:         // 帮派任务_战备
                        taskBuy();
                        break;
                    case 5:         // 帮派任务_物资
                        taskBuy();
                        break;
                    case 6:         // 帮派任务_除魔
                        chuMo();
                        break;
                    case 7:         // 帮派任务_药材
                        log.info("【{}】帮派任务_药材", getTaskName());
                        ThreadUtil.sleep2();
                        FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.帮派任务_药材_购买, 3);
                        ThreadUtil.sleep2();
                        FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.帮派任务_确认按钮, 3);
                        break;
                    case 8:         // 帮派任务_家园巡逻
                        log.info("【{}】帮派任务_家园巡逻", getTaskName());
                        ThreadUtil.sleep(10000);
                        findBattle();
                        break;
                    case 9:         // 帮派任务_帮派巡逻
                        log.info("【{}】帮派任务_帮派巡逻", getTaskName());
                        ThreadUtil.sleep(10000);
                        findBattle();
                        break;
                    case 10:        // 帮派任务_挑战帮主
                        log.info("【{}】帮派任务_挑战帮主", getTaskName());
                        ThreadUtil.sleep3();
                        FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.帮派任务_挑战帮主_确认, 3);
                        findBattle();
                        break;
                    default:
                        log.error("【{}】没有匹配到任务类型...", getTaskName());
                }

            }

//            if (FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.帮派任务_拜访, 1, sim)) {
//                baiFang();
//                continue;
//            }
//            if (FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.帮派任务_切磋, 1, sim)) {
//                qieCuo();
//                continue;
//            }
//            if (FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.帮派任务_宣读公告, 1, sim)) {
//                gongGao();
//                continue;
//            }
//            if (FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.帮派任务_粮草, 1, sim)) {
//                taskBuy();
//                continue;
//            }
//            if (FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.帮派任务_战备, 1, sim)) {
//                taskBuy();
//                continue;
//            }
//            if (FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.帮派任务_物资, 1, sim)) {
//                taskBuy();
//                continue;
//            }
//            if (FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.帮派任务_除魔, 1, sim)) {
//                chuMo();
//                continue;
//            }
//            if (FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.帮派任务_药材, 1, sim)) {
//                log.info("【{}】帮派任务_药材", getTaskName());
//                ThreadUtil.sleep2();
//                FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.帮派任务_药材_购买, 3);
//                ThreadUtil.sleep2();
//                FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.帮派任务_确认按钮);
//                continue;
//            }
//            if (FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.帮派任务_家园巡逻, 1, sim)) {
//                log.info("【{}】帮派任务_家园巡逻", getTaskName());
//                ThreadUtil.sleep(10000);
//                findBattle();
//                continue;
//            }
//            if (FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.帮派任务_帮派巡逻, 1, sim)) {
//                log.info("【{}】帮派任务_帮派巡逻", getTaskName());
//                ThreadUtil.sleep(10000);
//                findBattle();
//                continue;
//            }
//            if (FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.帮派任务_挑战帮主, 1, sim)) {
//                log.info("【{}】帮派任务_挑战帮主", getTaskName());
//                ThreadUtil.sleep3();
//                FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.帮派任务_挑战帮主_确认);
//                findBattle();
//                continue;
//            }
        }
    }

    @Override
    public Boolean after() throws Exception {
        return true;
    }

    @Override
    public String getTaskName() throws Exception {
        return "帮派任务";
    }

    private void taskBuy() throws Exception {
        log.info("【{}】帮派任务_粮草/战备/物资", getTaskName());
        // 先看包里有没有
        ThreadUtil.sleep3();
        if (FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.帮派任务_确认按钮, 3)) {
            ThreadUtil.sleep1();
            FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.公共_普通按钮);
            return;
        }

        ThreadUtil.sleep(10000);
        for (int i = 0; i < 5; i++) {
            if (FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.帮派任务_购买按钮, 1)) {
                // 防止延迟导致购买的物品被买走
                ThreadUtil.sleep1();
                if (!FuzzyPicUtil.findPic(hwnd_0, PicConstants.帮派任务_购买按钮, 1).isFinded()) {
                    break;
                }
                // 物品被买走，就再选择第一个买一次
                MouseUtil.lClickRandom(hwnd_0, Coord.BUY_FIRST_XY.getX(), Coord.BUY_FIRST_XY.getY());
                ThreadUtil.sleep1();;
            }
        }

        FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.帮派任务_确认按钮, 1, sim);

        ThreadUtil.sleep1();

        FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.公共_普通按钮, 1);
    }

    private void chuMo() throws Exception {
        log.info("【{}】帮派任务_除魔", getTaskName());
        ThreadUtil.sleep(30000);
        for (int i = 0; i < 10; i++) {
            if (FuzzyPicUtil.findPic(hwnd_0, PicConstants.帮派任务_除魔_完成, 1).isFinded()) {
                log.info("【{}】除魔完成", getTaskName());
                return;
            }
            ThreadUtil.sleep(6000);
        }
    }

    private void gongGao() throws Exception {
        log.info("【{}】帮派任务_宣读公告", getTaskName());
        ThreadUtil.sleep(10000);
        for (int i = 0; i < 10; i++) {
            ThreadUtil.sleep3();
            if (FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.公共_使用, 1)) {
                log.info("【{}】使用物品", getTaskName());
                ThreadUtil.sleep3();
                return;
            }
        }
    }

    private void baiFang() throws Exception {
        log.info("【{}】帮派任务_拜访", getTaskName());
        ThreadUtil.sleep(5000);
        for (int i = 0; i < 5; i++) {
            if (FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.帮派任务_确认按钮, 1, sim)) {
                return;
            }
            ThreadUtil.sleep3();
        }
    }

    private void qieCuo() throws Exception {
        log.info("【{}】帮派任务_切磋", getTaskName());
        ThreadUtil.sleep3();
        for (int i = 0; i < 10; i++) {
            if (FuzzyPicUtil.findAndClickRandom(hwnd_0, PicConstants.帮派任务_确认按钮, 1, sim)) {
                break;
            }
            ThreadUtil.sleep3();
        }
        ThreadUtil.sleep2();
        findBattle();
    }
}
